#include "Man.h"

Man::Man()
{
    _pos_x = 0;
    _pos_y = 0;

    _direction_x = 0;
    _direction_y = 0;

    _speed_x = 0;
    _speed_y = 0;

    _alive = true;
    _lives = 3;

    UpDown_on = true;
    LeftRight_on = true;
}

void Man::initialise()
{
    _pos_x = 650;
    _pos_y = 350;
    _direction_x = 0;
    _direction_y = 0;
    _speed_x = 5;
    _speed_y = 5;

    set_alive(true);
}

void Man::update()
{
    if( (_pos_x%50==0) && (_pos_y%50==0))
    {
        UpDown_on = true;
        LeftRight_on = true;
    }

    else if( (_pos_x%50==0) && !(_pos_y%50==0))
    {
        _direction_x = 0;
        LeftRight_on = false;
    }

    else if( !(_pos_x%50==0) && (_pos_y%50==0))
    {
        _direction_y = 0;
        UpDown_on = false;
    }

    //will move the man arround
    _pos_x += _speed_x * _direction_x;
    _pos_y += _speed_y * _direction_y;

    //check if trying to move man out the bounds of the game
    if (_pos_x < 0)
    {
        _pos_x = 0;
    }
    else if (_pos_x > GameWidth)
    {
        _pos_x = GameWidth;
    }

    if (_pos_y < 100)
    {
        _pos_y = 100;
    }
    else if (_pos_y > GameHeight)
    {
        _pos_y = GameHeight;
    }
}

void Man::move_up()
{
    if(UpDown_on) _direction_y = -1;
}

void Man::move_down()
{
    if(UpDown_on) _direction_y = 1;
    }

void Man::move_left()
{
    if(LeftRight_on) _direction_x = -1;
}

void Man::move_right()
{
    if(LeftRight_on) _direction_x = 1;
}

void Man::still_y()
{
    if ( _pos_x%50==0 && _pos_y%50==0 )
        _direction_y = 0;
}

void Man::still_x()
{
    if ( _pos_x%50==0 && _pos_y%50==0 )
        _direction_x = 0;
}

bool Man::collide_monster( int monster_pos_x, int monster_pos_y)
{
    if ( (_pos_x <= monster_pos_x+50)       &&      //monster approach from left
         (_pos_x+50 >= monster_pos_x+50 )   &&
         (_pos_y == monster_pos_y)          )
    {
        return true;
    }

    else if ( (_pos_x <= monster_pos_x)     &&
              (_pos_x+50 >= monster_pos_x)  &&      //monster approach from righ
              (_pos_y == monster_pos_y)     )
    {
        return true;
    }

    else if ( (_pos_x == monster_pos_x)     &&      //monster approach from above
              (_pos_y <= monster_pos_y+50)  &&
              (_pos_y+50 >= monster_pos_y+50))
    {
        return true;
    }

    else if ( (_pos_x == monster_pos_x)     &&      //monster approach from below
              (_pos_y+50 >= monster_pos_y)  &&
              (_pos_y <= monster_pos_y)  )
    {
        return true;
    }

    else return false;
}

void Man::loose_life()
{
    _lives --;

    _pos_x = 650;
    _pos_y = 350;

    if ( _lives <= 0 )
    {
        _alive = false;
    }
}
